package logic

import (
	"context"
	"errors"

	"jinrigaoping/rpc/sys/internal/svc"
	"jinrigaoping/rpc/sys/sys"

	"github.com/zeromicro/go-zero/core/logx"
)

type SysMenuDeleteLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSysMenuDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SysMenuDeleteLogic {
	return &SysMenuDeleteLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SysMenuDeleteLogic) SysMenuDelete(in *sys.SysMenuDeleteReq) (*sys.SysMenuDeleteRes, error) {

	count, err := l.svcCtx.SysMenuModel.ChildCount(l.ctx, in.MenuId)
	if err != nil {
		return nil, err
	}

	if count > 0 {
		return nil, errors.New("请先删除子菜单或按钮")
	}

	if err := l.svcCtx.SysMenuModel.Delete(l.ctx, in.MenuId); err != nil {
		return nil, err
	}

	return &sys.SysMenuDeleteRes{}, nil
}
